/*
 * author: Andriamananony, Meß, Falk
 */

package auftrag;

import java.util.ArrayList;
import java.util.Collections;

public class Variante {
	private ArrayList<Operation> arrOperation = new ArrayList<Operation>();
	private Produkt prodParent;
	private String strName;

	public Variante(Produkt p, int i) {
		prodParent = p;
		p.addVarianteToProdukt(this);
		strName = "V" + i;
	}

	public void addOperation(Operation opOperation) {
		this.arrOperation.add(opOperation);
		Collections.sort(this.arrOperation);

		if (opOperation.getStep() > 1) {
			for (Operation o : arrOperation) {
				if (o.getStep() == opOperation.getStep() - 1) {
					opOperation.setPreviousOperation(o);
				}
			}
		}
	}
	
	public int getMinDuration(){
		int i = 0;
		for(Operation o : arrOperation)
			i += o.getDuration();
		return i;
	}

	public int numOperations() {
		return this.arrOperation.size();
	}

	public Operation getOperation(int intIndex) {
		for (Operation o : arrOperation)
			if (o.getStep() == intIndex)
				return o;
		return null;
	}

	public int getEndDate() {
		int max = 0;
		for (Operation o : arrOperation) {
			if (o.getInterval() == null)
				return -1;
			max = Math.max(o.getInterval().getEnd(), max);
		}
		return max;
	}

	public ArrayList<Operation> getOperations() {
		return this.arrOperation;
	}

	public int getOperationSize() {
		return this.arrOperation.size();
	}

	public Produkt getParent() {
		return prodParent;
	}

	@Override
	public String toString() {
		return strName;
	}
}
